package org.xlx.xlx_oa.role_user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.role_user.convertor.RoleUserConvertor;
import org.xlx.xlx_oa.role_user.domain.AdmDepartment;
import org.xlx.xlx_oa.role_user.domain.AdmUser;
import org.xlx.xlx_oa.role_user.domain.dto.AdmDepartmentDto;
import org.xlx.xlx_oa.role_user.domain.vo.AdmDepartmentVo;
import org.xlx.xlx_oa.role_user.mapper.AdmDepartmentMapper;

import java.util.List;
import java.util.Objects;

@Service
public class AdmDepartmentService {
    private final RoleUserConvertor roleUserConvertor = RoleUserConvertor.INSTANCE;
    @Autowired
    private AdmDepartmentMapper departmentMapper;
    @Autowired
    private AdmUserService userService;

    /**
     * 保存
     */
    public Boolean saveDepartment(AdmDepartmentVo departmentVo) {
        AdmDepartment department = roleUserConvertor.convertVoToDepartment(departmentVo);
        Long id = department.getId();
        if (Objects.isNull(id)) {
            departmentMapper.insert(department);
        } else {
            departmentMapper.updateById(department);
        }
        return true;
    }

    /**
     * 删除
     */
    public Boolean removeDepartment(Long id) {
        AdmUser user = new AdmUser();
        user.setDepartmentId(id);
        Boolean empty = userService.checkDepartOrPositionEmpty(user);
        if (!empty) {
            throw new BusinessException("100008", "Still in use, please empty it first");
        }
        return departmentMapper.deleteById(id) > 0;
    }

    /**
     * 列表
     */
    public List<AdmDepartmentDto> getList(AdmDepartmentVo departmentVo) {
        AdmDepartment department = roleUserConvertor.convertVoToDepartment(departmentVo);
        Page<AdmDepartment> page = new Page<>(departmentVo.getPageNum(), departmentVo.getPageSize());
        QueryWrapper<AdmDepartment> queryWrapper = new QueryWrapper<>();
        if (!Objects.isNull(department.getTitle())) {
            queryWrapper.lambda().like(AdmDepartment::getTitle, department.getTitle());
        }
        departmentMapper.selectPage(page, queryWrapper);
        List<AdmDepartmentDto> departmentDtos = roleUserConvertor.convertToListDepartmentDto(page.getRecords());
        return departmentDtos;
    }

    /**
     * 详情
     */
    public AdmDepartmentDto getDetail(Long id) {
        AdmDepartment admDepartment = departmentMapper.selectById(id);
        return roleUserConvertor.convertToDepartmentDto(admDepartment);
    }

    /**
     * 数据总个数
     *
     * @param departmentVo
     * @return
     */
    public Integer getCount(AdmDepartmentVo departmentVo) {
        AdmDepartment department = roleUserConvertor.convertVoToDepartment(departmentVo);
        QueryWrapper<AdmDepartment> queryWrapper = new QueryWrapper<>();
        if (!Objects.isNull(department.getTitle())) {
            queryWrapper.lambda().like(AdmDepartment::getTitle, department.getTitle());
        }
        return departmentMapper.selectCount(queryWrapper);
    }
}
